<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\controller\Common;
use app\service\billService;
use app\service\commonService;
use app\service\roominfoService;
use app\service\apiService;
use think\Db;
use printing\printDemo;


class Billpay extends Common{
    //根据当前账号信息查询token
    public function pidTokenId(){
        $id = session('id');
        //根据ID查询用户信息
        $adminId = (new commonService())->adminIdInfo($id);
        $usertype = $adminId['usertype'];//角色类型
        session("usertype",$usertype);
        $department = $adminId['department'];//所属部门
        if($usertype == 0){//超级管理员
            //查询所有服务商
            
            $com['service_id'] = (new commonService())->serviceList();
            //查询所有物业
            $com['property_id'] = (new commonService())->propertyList();
            //查询所有小区
            $com['community_id'] = (new commonService())->communityList();
            $com["usertype"]=0;
            $com['com_id'] = Db::name('community')->column('id');;
        }else if($usertype == 3){//服务商
            $com['service_id'] = $department;
            $com["usertype"]=3;
            //查询所有物业
            $com['property_id'] = (new commonService())->serviceIdProperty($department);
            //查询所有小区
            $com['community_id'] = (new commonService())->propertyIdCommunity($com['property_id']);
            $com['com_id']= Db::name('community')->where('property_id','in',$com['property_id'])->column('id');
        }else if($usertype == 1){//物业
            //查询所有小区
            $com["usertype"]=1;
            $com['property_id'] = $department;
            $com['community_id'] = (new commonService())->propertyIdCommunity($department);
            $com['com_id']= Db::name('community')->where('property_id','in',$department)->column('id');
        }else if($usertype == 2){//小区
            $com["usertype"]=2;
            $com['community_id'] = (new commonService())->communityIdCommunity($department);
            $com['com_id']=Db::name('community')->where('id',$department)->value('id');
        }
        return $com;
    }

    //查询直付通账单
    public function getList(){
        //查询小区信息
        $com = $this->pidTokenId();

        $whe['community_id'] = array('in',$com['community_id']);
        $communitylist = (new roominfoService())->communityList($whe);
        $community_id = $com['community_id'];
        $this->assign('communitylist',$communitylist);

        //搜索时
        $data = request()->get();
        $address="";
        if (!empty($data['community_id'])){
            $groups = array_unique((new roominfoService())->groupList($data['community_id']));
            $this->assign('groups', array_values($groups));
            if (!empty($data['group'])) {
                $address=$data['group'];
                $buildings = array_unique((new roominfoService())->buildingList($data['community_id'],$data['group']));
                sort($buildings);
                $this->assign('buildings',$buildings);
                if (!empty($data['building'])) {
                    $address=$data['group']."-".$data["building"];
                    $units = array_unique((new roominfoService())->unitList($data['community_id'], $data['group'],$data['building']));
                    sort($units);
                    $this->assign('units',$units);
                    if(!empty($data['unit'])){
                        $address=$data['group']."-".$data["building"]."-".$data['unit'];
                        $rooms = array_unique((new roominfoService())->roomList($data['community_id'], $data['group'],$data['building'],$data['unit']));
                        sort($rooms);
                        $this->assign('rooms',$rooms);
                        if(!empty($data["room"])){
                            $address=$data['group']."-".$data["building"]."-".$data['unit']."-".$data["room"];
                        }
                    }
                }
            }

        }
        $where["scenes"]=5;
        $where["order_status"]=1;
        if (!empty($data)){
            if(!empty($data['bill_entry_id'])){
                $where['bill_entry_id'] = $data['bill_entry_id'];//账单编号
            }else{
                $where['bill_entry_id'] = "";
            }
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }

            if(!empty($data['room_address'])){
                $where['room_address'] = array("like","%".$data['room_address']."%");//楼栋
            }else{
                $where['room_address'] = "";
            }
            if(!empty($address)){
                $where['room_address'] = array("like","%".$address."%");//楼栋
            }
            if(!empty($data['cost_type'])){
                $where['cost_type'] = $data['cost_type'];//费用类型
            }else{
                $where['cost_type'] = "";
            }
            if(!empty($data['status'])){
                $where['status'] = $data['status'];//账单状态
            }else{
                $where['status'] = "";
            }
            if(!empty($data['tongbu'])){
                $where['tongbu'] = $data['tongbu'];//账单状态
            }else{
                $where['tongbu'] = "";
            }


            if(!empty($data['owner_name'])){
                $whereroom['owner_name'] = array('like','%'.$data['owner_name'].'%');//业主姓名
            }else{
                $whereroom['owner_name'] = "";
            }
            if(!empty($data['owner_phone'])){
                $whereroom['owner_phone'] = $data['owner_phone'];//业主手机号
            }else{
                $whereroom['owner_phone'] = "";
            }
            //var_dump($where);
            $this->assign('whereroom',$whereroom);
            $whereroom = array_filter($whereroom);
            if(!empty($whereroom)){
                $where['out_room_id'] = array('in',(new billService())->whereRoom($community_id,$whereroom));
            }


            $this->assign('where',$where);
            $where = array_filter($where);


        }

        $page = request()->has('page') ? request()->get('page') : 1;

        $export = $where;

        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        $this->assign('export',$export);
        //查询账单数据
        $billlist =Db::name('billpay')
                ->where('community_id', 'in', $community_id)
                ->where($where)
                ->order('id desc')->paginate(200, false, [
                    'page'  =>  $page,
                    'query' =>  $where
                ]);

        //查询账单条数
        $number =Db::name('billpay')
            ->where('community_id', 'in', $community_id)
            ->where($where)->count();

        //$billsum=0;

        if(!empty($billlist)){
            foreach($billlist as $bill){
                //根据小区编号查询小区名称
                $community = (new billService())->communityName($bill['community_id']);
                $bill['community_id'] = $community['community_name'];
                //根据房屋门牌号查询业主信息
                $bill['owner_name'] = (new billService())->billOwnerName($bill['out_room_id']);
                //数据从新存入数组
                $list[] = $bill;
                $this->assign('list',$list);
            }
        }else{
            $this->assign('list',$billlist);
        }
        if(!isset($list)){
            $this->assign('list',$billlist);
        }
        //判断是否有数据

        $this->assign('billlist',$billlist);


        $this->assign('number',$number);

        //var_dump($data);
        foreach ($data as $k => $v) {
            $this->assign($k, $v);
        }

        return $this->fetch('bill_list');

    }

    
}